<?php

require_once('Adept/Component/IPersistent.php');

class Adept_Component_Control extends Adept_Component_Composite implements Adept_Comonent_IPersistent    
{
    
    protected $accessKey = null;
    protected $alt = null;
    protected $disabled = null;
    protected $tabIndex = null;    
    protected $title = null;
    protected $class = null;
    protected $style = null;

    protected $listeners = array();
    
    protected $browserEvents = array();
    
    protected $persistent = false;

    /**
     * @param string $eventType Event type
     * @param Adept_ClassKit_Delegate $listener
     */
    public function addListener($eventType, $listener) 
    {
        $this->listeners[$eventType][] = $listener;
    }
    
    public function addListenerBinding($eventType, $binding) 
    {
        if (is_string($binding)) {
            $listener = Adept_Binding_Factory::createMethodBinding($binding);
        } else {
            $listener = $binding;
        }
        $this->listeners[$eventType][] = $listener;
    }
    
    
    public function getListeners($eventType) 
    {
        return isset($this->listeners[$eventType]) ? $this->listeners[$eventType] : array();
    }    
    
    public function invokeListenersChain($eventType, $arguments = array()) 
    {
        return Adept_ClassKit_Delegate_List::invokeChain($this->getListeners($eventType), $arguments);
    }    
    
    public function addValidatePhase() 
    {
        $this->getRoot()->addPhaseListener(Adept_Lifecycle::VALIDATE_PHASE, 
            new Adept_ClassKit_Delegate($this, 'processValidate'));        
    }    
    
    public function addUpdatePhase() 
    {
        $this->getRoot()->addPhaseListener(Adept_Lifecycle::UPDATE_PHASE, 
            new Adept_ClassKit_Delegate($this, 'processUpdate'));                
    }    
    
    public function addEventPhase() 
    {
        $this->getRoot()->addPhaseListener(Adept_Lifecycle::EVENT_PHASE, 
            new Adept_ClassKit_Delegate($this, 'processEvent'));                        
    }    
    
    public function processRestore() 
    {
        if ($this->isPersistent()) {
            $state = $session->getValue($this->getUniqueId());        
            if ($state !== null) {
                $this->restoreState($state);
            }
        }
    }    
    
    public function processValidate() 
    {
        
    }    
    
    public function processUpdate() 
    {
        
    }    
    
    public function processEvent() 
    {
        
    }    
    
    public function processSave() 
    {
        if ($this->isPersistent()) {
            $session->setValue($this->getUniqueId(), $this->saveState());        
        }
    }    
        

    public function isPersistent() 
    {
        return $this->persistent;
    }
    
    public function setPersistent($persistent) 
    {
        $this->persistent = $persistent;
    }    
    
    public function saveState() 
    {
        return array();
    }
    
    public function restoreState($state) 
    {
    }
    
    public function getParentForm() 
    {
    	return $this->findParentByClass('Adept_Component_Form');
    }	
        
    public function getAccessKey() 
    {
        if (!is_null($this->accessKey)) {
            return $this->accessKey;
        }
        return $this->getValueOfBinding('accessKey', null);
    } 
    
    public function setAccessKey($accessKey) 
    {
        $this->accessKey = $accessKey;
    }
    
    public function getAlt() 
    {
        if (!is_null($this->alt)) {
            return $this->alt;
        }
        return $this->getValueOfBinding('alt', null);
    }
    
    public function setAlt($alt) 
    {
        $this->alt = $alt;
    }
        
    public function getBroswerEvent($event) 
    {
        $event = strtolower($event);
        if (isset($this->browserEvents[$event])) {
            return $this->browserEvents[$event];
        }
        return $this->getValueOfBinding($event, null);
    }

    public function setBrowserEvent($event, $handler) 
    {
        $this->browserEvents[strtolower($event)] = $handler;
    }    
    
    public function getBrowserEvents() 
    {
        $events = array();
        foreach (array_keys($this->browserEvents) as $key) {
            $events[strtolower($key)] = $this->getBroswerEvent($key);
        }
        return $events;
    }	

    public function getClass() 
    {
        if (!is_null($this->class)) {
            return $this->class;
        }
        return $this->getValueOfBinding('class', null);
    }
    
    public function setClass($class) 
    {
        $this->class = $class;
    }

    public function isDisabled() 
    {
        if (!is_null($this->disabled)) {
            return $this->disabled;
        }
        return $this->getValueOfBinding('disabled', false);
    }
    
    public function setDisabled($disabled) 
    {
        $this->disabled = $disabled;
    }    
    
    public function getTabIndex() 
    {
        if (!is_null($this->tabIndex)) {
            return $this->tabIndex;
        }
        return $this->getValueOfBinding('tabIndex', null);
    }
    
    public function setTabIndex($tabIndex) 
    {
        $this->tabIndex = $tabIndex;
    }
        
    public function getTitle() 
    {
        if (!is_null($this->title)) {
            return $this->title;
        }
        return $this->getValueOfBinding('title', null);
    }
    
    public function setTitle($title) 
    {
        $this->title = $title;
    }

    public function getStyle() 
    {
        if (!is_null($this->style)) {
            return $this->style;
        }
        return $this->getValueOfBinding('style', null);
    }
    
    public function setStyle($style) 
    {
        $this->style = $style;
    }
    
}
